package com.netease.xone.view;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import protocol.meta.AppImage;
import Utils.StackTraceUtil;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.netease.log.NTLog;
import com.netease.xone.R;
import com.netease.xone.activity.ActivityImageBrowser;
import com.netease.xone.fragment.FragmentImageBrowser;
import com.netease.xone.widget.LoadingImageView;


public class PopupImagePreview implements OnClickListener, OnDismissListener {
    private static final String TAG = PopupImagePreview.class.getSimpleName();
    
    private int mHeight;
    private LoadingImageView mImage;
    private TextView mButton;
    
    private PopupWindow mPopupWindow;

    public static void luach(Context context, AppImage appImage) {
        if (null == appImage) {
            return;
        }
        
        View root = View.inflate(context, R.layout.popup_image_preview, null);
        new PopupImagePreview(root).lauch(appImage);
    }
    
    public PopupImagePreview(View v) {
        mHeight = v.getResources().getDimensionPixelSize(R.dimen.detail_size_480);
        mImage = (LoadingImageView)v.findViewById(R.id.image);
        mButton = (TextView)v.findViewById(R.id.button);
        
        v.setOnClickListener(this);
        mImage.setServerClipSize(0, mHeight);
        mButton.setOnClickListener(this);
        
        mPopupWindow = new PopupWindow(v, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.showAtLocation(mPopupWindow.getContentView(), Gravity.CENTER, 0, 0);
        mPopupWindow.setOnDismissListener(this);
    }

    public void lauch(AppImage appImage) {
        mImage.setLoadingImage(appImage.getUrl());
        mButton.setTag(appImage);
    }
    
    @Override
    public void onClick(View v) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        
       if (v instanceof TextView) {
            AppImage appImage = (AppImage)v.getTag();
            List<AppImage> list = new ArrayList<AppImage>();
            list.add(appImage);
            
            Gson gson = new Gson();
            Type collectionType = new TypeToken<List<AppImage>>(){}.getType();
            String value = gson.toJson(list, collectionType);
            ActivityImageBrowser.startActivity(v.getContext(), 0, FragmentImageBrowser.TYPE_IMAGE, value);
        }
        
        clear();
    }
    
    public void clear() {
        
        if (null != mPopupWindow && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
            mPopupWindow = null;
        }
        
        mImage = null;
        if (null != mButton) {
            mButton.setOnClickListener(null);
        }
        mButton = null;
    }


    @Override
    public void onDismiss() {
        mPopupWindow = null;
        clear();
    }
}
